(defun tiw-new-note ()
  (interactive)
  (save-excursion
    (goto-char 0)
    (if (looking-at "-")
	()
      (insert "-*- mode: org -*-\n")))
  (insert "* ")
  (template-insert-time "%d-%m-%Y %H:%M")
  (insert "\n"))


(defun insert-time ()
  (interactive)  
  (template-insert-time "%Y-%m-%d"))
(defun insert-arrow ()
  (interactive)
  (insert "->"))
(defun insert-xtime ()
  (interactive)
  (template-insert-time "%H:%M"))

(defun insert-ftime ()
  (interactive)
  (insert-time)
  (insert " ")
  (insert-xtime))

(defun mps ()
  (interactive)
  (if (not (string-match "^\\(.*\\)\\.tex$" (buffer-file-name)))
      (message "wrong buffer for this command")
    (if (string-match "^XEmacs" (emacs-version))
	(shell-command (concat "mgv " (buffer-file-name)) " *latex-output*")
      (shell-command (concat "mgv " (buffer-file-name)) " *latex-output*" " *latex-errors*"))))




(defun inputSchedule (taskArt description)
  (interactive "sArts: 0. moveon 1. moveonline 2. movein 3. moveonnet 4. pause 5. misc  6. Reading 7. tools \nsDescription:")
  (if (string= "1" taskArt)
      (setq Art "moveonline")
    (if (string= "2" taskArt)
	(setq Art "movein")
      (if (string= "3" taskArt)
	  (setq Art "moveonnet")
		(if (string= "4" taskArt)
		    (setq Art "pause")
		  (if (string= "0" taskArt)
		      (setq Art "moveon")
		    (if (string="6" taskArt)
			(setq Art "reading")
		      (if (string="7" taskArt)
			  (setq Art "tools")
		      (if(string= "5" taskArt)
			  (setq Art "misc")
			(setq Art taskArt))) ))))))
    
    (insert "|")
    (insert-xtime)
    (insert 
     (concat "|" Art ": " description "\n"))
    (insert-xtime))
  
(defun insertCvsLogTemplate ()
  (interactive)
  (if (or (string= (buffer-name) "*VC-log*") (string= (buffer-name) "*cvs-commit*"))
      (if (file-exists-p "CVS/Template")
	  (insert-file-contents "CVS/Template"))))

(defun addWord ()
  (interactive)
  (append-to-file (region-beginning) (region-end) "/home/tiw/.vocabulary")
  (keyboard-quit))

(defun tiw-view-pr ()
  (interactive)
  (setq cur (point))
  (save-excursion
    (setq startpos (+ (re-search-backward "[^0-9]") 1))
    (goto-char cur) 
    (setq endpos (- (re-search-forward  "[^0-9]") 1))
    (view-pr
     (buffer-substring-no-properties 
      startpos endpos))))

(defun tiw-cvs-clean ()
  (interactive)
  (save-excursion
    (goto-char 0)
    (while (progn
	     (if (looking-at "^CVS:")
		 (kill-line))
	     (forward-line 1)
	     (not (looking-at "^$"))))))

(defun tiw-add-linenum ()
  (interactive)
  (save-excursion
    (setq counter 1)
    (setq lines (count-lines (region-beginning) (region-end)))
    (setq lines (+ 1 lines))
    (goto-char (region-beginning))
   (while (progn
	     (if (< counter 10)
		 (setq num (concat "0" (number-to-string counter)))
	       (setq num (number-to-string counter)))
	     (insert (concat num ""))
	     (forward-line 1)
	     (setq counter (+ counter 1))
	     (< counter lines)))))

(defun tiw-cdade ()
  (interactive)
  (dired "/project/ods/build/"))

(defun tiw-init-planner ()
  "add -*- planner -*- at the beginning of the plan"
  (interactive)
  (save-excursion
    (goto-char 0)
    (if (looking-at "-")
	()
      (insert "-*- planner -*-\n"))))

(defun tiw-init-schedule ()
  (interactive)
  (setq pp (point))
  (setq inited nil)
  (goto-char 0)
  (setq finded nil)
  (while (and (not finded) (not (= (point) (point-max))))
    (if (looking-at "\\* Schedule.*")
	(progn 
	  (forward-line 1)	
	  (setq isblank (looking-at "^[ \t]*$"))
	  (if isblank
	      (progn
		(template-insert-time "%H:%M")
		(setq inited t)))
	  (forward-line -1)
	  (end-of-line)
	  (setq finded t)))
    (forward-line 1))
  (end-of-line)
  (if (not inited)
      (goto-char pp)))


(defun tiw-del-pair ()
;;  "delete contents within pairs e.g (..) when
;;(,[, [ or \" is under the cursor" ;;"
  (interactive)
  (if (looking-at "\\(\"\\|\(\\|\\[\\|\{\\)")
      (progn
	(if (looking-at "\"") ;;"
	    (progn
	      (forward-char)
	      (kill-region (point) (progn
				     (search-forward "\"")
				     (- (point) 1))))
	  (progn
	    (setq beginp (point))
	    (setq endp (progn (forward-list) (point)))
	    (kill-region (+ beginp 1) (- endp 1))))
	(backward-char))
    (when (looking-at "\\(\)\\|\\]\\|\}\\)")
      (setq beginp (point))
      (forward-char)
      (setq endp (progn (backward-list) (point)))
      (kill-region (+ 1 endp) beginp)
      (forward-char))))

(defun tiw-goto-first-char ()
  (beginning-of-line)
  (when (re-search-forward "\[^\[:blank:\]\]" (tiw-line-end-position) t)
    (backward-char)))

(defun tiw-goto-last-char ()
  (end-of-line)
  (when (re-search-backward "\[^\[:blank:\]\]" (tiw-line-beginning-position) t)
    (forward-char)))
  

(defun tiw-surround-sprlist ()
  "surround the spr number with latex symbol for the test 
summary of spr test"
  (interactive)
  (tiw-goto-first-char)
  (while (looking-at "\\b[0-9]+\\b")
    (progn
      ;;      (tiw-goto-first-char)
      (insert "\\assed{")
      (tiw-goto-last-char)
      (insert "}{}{ $\\to$ }")
      (forward-line 1)
      (tiw-goto-first-char))))


(defun tiw-insert-sprlist ()
  "read spr list form the file .sprlist and surround all
pr number with latex symbol for the test summary"
  (interactive)
  (setq begin-pos (point))
  (insert-file-contents "/home/tiw/.sprlist")
  (goto-char begin-pos)
  (tiw-surround-sprlist)
  (setq end-pos (point))
  (goto-char begin-pos)
  (indent-region begin-pos end-pos nil))



(defun tiw-copy-latex-makefile ()
  "copy a makefile with which a ps file will be made"
  (interactive)
  (copy-file "~/home/tiw/work/reports/4.0.8/Makfile" ""))

(defun tiw-zap-to-char (arg char)
  "same as zap-to-char in simple, but do not delete the selected char"
  (interactive "p\ncZap to char: ")
  (kill-region (point) (progn
			 (search-forward (char-to-string char) nil nil arg)
;			 (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
			 (- (point) 1)))
  (backward-char 1))


(defun tiw-endline-return ()
  "goto ending of line and return"
  (interactive)
  (end-of-line)
  (newline-and-indent))

(defun tiw-comment ()
  "comment/uncomment a line"
  (interactive)
  (save-excursion
;;    (if (region-active-p)
    (if (eq nil mark-active)
	(tiw-comment-line)
      (tiw-comment-region))))


(defun tiw-comment-line ()
  "comment or uncomment a line according to 
the first charachter"
  (save-excursion
    (beginning-of-line)
    (if (looking-at comment-start)
  	(tiw-uncomment-region (tiw-line-beginning-position) (tiw-line-end-position))
      (comment-region  (tiw-line-beginning-position) (tiw-line-end-position) nil))))

(defun tiw-line-beginning-position ()
  (if (string-match "^XEmacs" (emacs-version))
      (progn
	(save-excursion
	  (beginning-of-line)
	  (point)))
    (line-beginning-position)))

(defun tiw-line-end-position ()
  (if (string-match "^XEmacs" (emacs-version))
      (progn
	(save-excursion
	  (end-of-line)
	  (point)))
    (line-end-position)))

(defun tiw-uncomment-region(b-p e-p)
  (if (string-match "^XEmacs" (emacs-version))
      (comment-region b-p e-p -1)
    (uncomment-region b-p e-p nil)))

(defun tiw-comment-region ()
  "comment or uncomment a region according to 
the first character"
  (interactive)
  (let ((b-p (region-beginning))
		(e-p (region-end)))
    (save-excursion
      (goto-char (region-beginning))
      (if (looking-at comment-start)
		  (tiw-uncomment-region b-p e-p)
		(comment-region b-p e-p nil)))))

(defun tiw-insert-summary-detail ()
  (interactive)
  (insert "\\underline{\\Large \\sf Backstrace of INRADAR test}\\\\[0.3cm]\n")
  (insert "\\begin{verbatim}\n")
  (insert "\\end{verbatim}\n"))

(defun tiw-surrend-string ()
  (interactive)
  (let ((b-p (region-beginning))
		(e-p (region-end)))
	(save-excursion
	  (goto-char e-p)
	  (insert "///@endcond\n")
	  (goto-char b-p)
	  (insert "///@cond\n"))))



(defun tiw-beginning-of-line () 
  "If the point is not on beginning of current line, move point 
to beginning of current line, as 'beginning-of-line' does.  
If the point already is on the beginning of current line, then 
move the point to the first non-space character, if it exists." 
  (interactive) 
  (if (not (eq (point) (line-beginning-position))) 
      (beginning-of-line) 
    (when (re-search-forward "\[^\[:blank:\]　\]" (line-end-position) t) 
      (backward-char)) 
    ))
(global-set-key (kbd "C-a") 'tiw-beginning-of-line)


(defun gtd ()
  (interactive)
  (find-file "~/Document/org/2008.org")
)

 
